package problem;

import java.util.Arrays;

/**
 * @author RunningShrimp
 * @date 2021/6/10  18:47
 * @see <a href="https://leetcode-cn.com/problems/sort-colors/">75. 颜色分类</a>
 */
public class SortColors {
    public void sortColors(int[] nums) {
        int[] colors = new int[]{0, 0, 0};
        for (int num : nums) {
            colors[num]++;
        }
        int index = 0;
        for (int i = 0; i < colors.length; i++) {
            for (int j = 0; j < colors[i]; j++) {
                nums[index] = i;
                index++;
            }
        }
    }

    public void sortColors2(int[] nums) {
        int zero = -1;
        int two = nums.length;
        for (int i = 0; i < two; ) {
            if (nums[i] == 1) {
                i++;
            } else if (nums[i] == 2) {
                two--;
                int temp = nums[i];
                nums[i] = nums[two];
                nums[two] = temp;
            } else {
                zero++;
                int temp = nums[i];
                nums[i] = nums[zero];
                nums[zero] = temp;
                i++;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{2, 0, 2, 1, 1, 0};
        new SortColors().sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}
